<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>


        //封装ajax，返回一个 Promise对象
        function sendAjax(url){
            return new Promise((resolve,reject)=>{
                //1.创建对象
                let xhr = new XMLHttpRequest(); 
                //2。初始化发送请求
                xhr.open("GET",url)
                //3.参数
                // xhr.setRequestHeader([name],[value])

                //4.发送
                xhr.send();
                //5. 事件绑定
                xhr.onreadystatechange = function(){ 
                    if(xhr.readyState == 4){
                        //表示请求已经返回
                        if(xhr.status >= 200 && xhr.status <= 300){
                            resolve(xhr.response)
                        }else{
                            reject(xhr.status)
                        }
                    }
                }
            })
        }


        sendAjax("https://api.apiopen.top/getJoke").then(value=>{
            console.log(value)
        },reason=>{
            console.log(reason)
        })
       

        async function main(){
            let result = await sendAjax("https://api.apiopen.top/getJoke")
            console.log(result)
        }   
        main()



    </script>
</body>
</html>